﻿using BeerStats.Data;
using BeerStats.WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace BeerStats.WebAPI.Controllers
{
    public class BeersController : BaseApiController
    {
        [HttpGet]
        [ActionName("all")]
        public IQueryable<BeerFullModel> GetAll()
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new BeerStatsDbContext();
                var beers = (from beer in context.Beers
                             select new BeerFullModel()
                             {
                                 BeerId = beer.BeerId,
                                 AlcoholByVolume = beer.AlcoholByVolume,
                                 BeerName = beer.Name,
                                 BreweryName = beer.BreweryName,
                                 Description = beer.Description,
                                 LabelSource = beer.LabelSource,
                                 ImageSource = beer.ImageSource,
                                 BeerType = beer.BeerType.Name,
                                 Rating = beer.Rating
                             }).AsQueryable();

                return beers;

            });
        }

        [HttpGet]
        public BeerFullModel GetBeerDesrpition(int beerId)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new BeerStatsDbContext();
                var beers = (from beer in context.Beers
                             where beer.BeerId == beerId
                             select new BeerFullModel()
                             {
                                 BeerId = beer.BeerId,
                                 AlcoholByVolume = beer.AlcoholByVolume,
                                 BeerName = beer.Name,
                                 BreweryName = beer.BreweryName,
                                 Description = beer.Description,
                                 ImageSource = beer.ImageSource,
                                 BeerType = beer.BeerType.Name,
                                 Rating = beer.Rating
                             }).FirstOrDefault();

                return beers;

            });
        }
    }
}
